package com.example.palette.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.example.palette.R;


public class VinylView extends View {
    Bitmap mVinylHandBitmap;
    Matrix mMatrix;
    float mCurrentDegree;
    float mOffset;
    float mScale = -1;

    float mDefaultMaxDegree = -25f;
    ValueAnimator mStartRotationAnimator;
    ValueAnimator mStopRotationAnimator;

    public VinylView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mMatrix = new Matrix();
        LinearInterpolator linearInterpolator = new LinearInterpolator();
        mVinylHandBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.vinyl_hand);
        mOffset = mVinylHandBitmap.getWidth() * 0.18f;
        mStartRotationAnimator = ValueAnimator.ofFloat(0, mDefaultMaxDegree);
        mStartRotationAnimator.setDuration(500);
        mStartRotationAnimator.setInterpolator(linearInterpolator);
        mStartRotationAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurrentDegree = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        mStopRotationAnimator = ValueAnimator.ofFloat(mDefaultMaxDegree, 0);
        mStopRotationAnimator.setDuration(500);
        mStopRotationAnimator.setInterpolator(linearInterpolator);
        mStopRotationAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurrentDegree = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w > 0 && h > 0) {
            int bitmapMaxBorder = Math.max(mVinylHandBitmap.getWidth() + getWidth() / 2, mVinylHandBitmap.getHeight() + getWidth() / 2);
            if (bitmapMaxBorder > w || bitmapMaxBorder > h) {
                int viewMinBorder = Math.min(w, h);
                mScale = viewMinBorder / (float) bitmapMaxBorder;
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mScale != -1) {
            canvas.scale(mScale, mScale);
        }
        canvas.rotate(-mDefaultMaxDegree, getWidth() / 2, getHeight() / 2);
        mMatrix.setRotate(mCurrentDegree, mOffset, mOffset);
        canvas.drawBitmap(mVinylHandBitmap, mMatrix, null);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mVinylHandBitmap != null) {
            mVinylHandBitmap.recycle();
            mVinylHandBitmap = null;
        }
        if (mStartRotationAnimator != null) {
            mStartRotationAnimator.removeAllListeners();
            mStartRotationAnimator.removeAllUpdateListeners();
            mStartRotationAnimator.cancel();
            mStartRotationAnimator = null;
        }
        if (mStopRotationAnimator != null) {
            mStopRotationAnimator.removeAllListeners();
            mStopRotationAnimator.removeAllUpdateListeners();
            mStopRotationAnimator.cancel();
            mStopRotationAnimator = null;
        }
    }

    public void startAnim() {
        if (mStopRotationAnimator.isStarted()) {
            if (mStopRotationAnimator.isRunning()) {
                mStopRotationAnimator.pause();
                mStartRotationAnimator.setFloatValues(mCurrentDegree, mDefaultMaxDegree);
            } else {
                mStartRotationAnimator.setFloatValues(0, mDefaultMaxDegree);
            }
        } else {
            if (mCurrentDegree == mDefaultMaxDegree) {
                mStartRotationAnimator.setFloatValues(mDefaultMaxDegree, mDefaultMaxDegree);
            } else {
                mStartRotationAnimator.setFloatValues(0, mDefaultMaxDegree);
            }
        }
        if (!mStartRotationAnimator.isStarted() || !mStartRotationAnimator.isRunning() || mStartRotationAnimator.isPaused()) {
            mStartRotationAnimator.start();
        }
    }

    public void stopAnim() {
        if (mStartRotationAnimator.isStarted()) {
            if (mStartRotationAnimator.isRunning()) {
                mStartRotationAnimator.pause();
                mStopRotationAnimator.setFloatValues(mCurrentDegree, 0);
            } else {
                mStopRotationAnimator.setFloatValues(mDefaultMaxDegree, 0);
            }
        } else {
            if (mCurrentDegree == 0) {
                mStopRotationAnimator.setFloatValues(0, 0);
            } else {
                mStopRotationAnimator.setFloatValues(mDefaultMaxDegree, 0);
            }
        }
        if (!mStopRotationAnimator.isStarted() || !mStopRotationAnimator.isRunning() || mStopRotationAnimator.isPaused()) {
            mStopRotationAnimator.start();
        }
    }
}
